import { createApp } from 'vue'
import './style.css'
import 'primeicons/primeicons.css'
import './assets/css/bpmn-i18n.css'
import './assets/css/bpmn-direct-i18n.css'
import './utils/bpmn-i18n.js'
import './utils/bpmn-direct-i18n.js'
import './utils/bpmn-simple-i18n.js'
import App from './App.vue'

// 导入国际化配置
import i18n from './i18n'

// 导入路由
import router from './router'

// 导入 Pinia
import pinia from './store'
import { useUserStore } from './store/modules/user'

// 导入 PrimeVue
import PrimeVue from 'primevue/config'
import Aura from '@primevue/themes/aura'

// 导入 PrimeVue组件
import Button from 'primevue/button'
import InputText from 'primevue/inputtext'
import Menu from 'primevue/menu'
import DataTable from 'primevue/datatable'
import Column from 'primevue/column'
import Dropdown from 'primevue/dropdown'
import Breadcrumb from 'primevue/breadcrumb'
import Badge from 'primevue/badge'
import Chart from 'primevue/chart'
import Textarea from 'primevue/textarea'
import InputSwitch from 'primevue/inputswitch'
import Dialog from 'primevue/dialog'
import Password from 'primevue/password'
import MultiSelect from 'primevue/multiselect'
import Tag from 'primevue/tag'
import InputNumber from 'primevue/inputnumber'
import Calendar from 'primevue/calendar'
import Paginator from 'primevue/paginator'
import Tabs from 'primevue/tabs'
import TabView from 'primevue/tabview'
import TabPanel from 'primevue/tabpanel'

// 创建应用实例
const app = createApp(App)

// 使用 PrimeVue
app.use(PrimeVue, {
  theme: {
    preset: Aura,
    options: {
      darkModeSelector: '.dark'
    }
  }
})

// 注册 PrimeVue 组件
app.component('Button', Button)
app.component('InputText', InputText)
app.component('Menu', Menu)
app.component('DataTable', DataTable)
app.component('Column', Column)
app.component('Dropdown', Dropdown)
app.component('Breadcrumb', Breadcrumb)
app.component('Badge', Badge)
app.component('Chart', Chart)
app.component('Textarea', Textarea)
app.component('InputSwitch', InputSwitch)
app.component('Dialog', Dialog)
app.component('Password', Password)
app.component('MultiSelect', MultiSelect)
app.component('Tag', Tag)
app.component('InputNumber', InputNumber)
app.component('Calendar', Calendar)
app.component('Paginator', Paginator)
app.component('Tabs', Tabs)
app.component('TabView', TabView)
app.component('TabPanel', TabPanel)

// 使用 Pinia
app.use(pinia)

// 使用路由
app.use(router)

// 使用国际化
app.use(i18n)

// Mock 服务已移除

// 应用初始化
const initApp = async () => {
  // 挂载应用
  app.mount('#app')
  
  // 开发环境下：检查URL参数，如果有clear-auth参数则清除localStorage中的token
  if (import.meta.env.DEV) {
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('clear-auth')) {
      localStorage.removeItem('token');
      // 重定向到登录页
      router.push('/login');
      return; // 终止初始化流程
    }
    
    // 开发环境：如果访问登录页，自动清除认证状态
    if (window.location.pathname === '/login') {
      localStorage.removeItem('token');
      const userStore = useUserStore();
      userStore.token = null;
      userStore.isAuthenticated = false;
      userStore.userInfo = null;
    }
  }
  
  // 检查认证状态（仅在非登录页时）
  if (window.location.pathname !== '/login') {
    const userStore = useUserStore();
    const token = localStorage.getItem('token');
    if (token && !userStore.userInfo) {
      try {
        await userStore.initUserInfo();
      } catch (error) {
        // 静默处理错误
      }
    }
  }
  
  // 开发环境下的登录测试
  // 注意：如需访问登录页，请将AUTO_LOGIN设置为false
  const AUTO_LOGIN = false; // 开发环境自动登录开关
  
  if (import.meta.env.DEV && AUTO_LOGIN && (!localStorage.getItem('token') || !userStore.isAuthenticated)) {
    // 2秒后自动登录
    setTimeout(async () => {
      await userStore.login('admin', '123456');
    }, 2000);
  }
}

// 调用初始化函数
initApp();
